﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;


namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class ExecutiveDAL : IExecutiveDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public ExecutiveDAL()
        {

        }
        //public ExecutiveInfo GetExecutiveInfo(Nullable<int> seq )
        //{
        //    string strSQL;

        //    ExecutiveInfo Info = null;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(2);

        //        dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
        //        dbManager.AddParameters(1, "@seq", seq);

        //        strSQL = "SELECT EXEC_POS_SEQ, EXEC_POS_NAME, EXEC_POS_NAME_SHORT, REMARKS"
        //            + " FROM HRM_MP_EXEC_POSITIONS"
        //            + " WHERE RECORD_STATUS not like @DeleteRecord"
        //                + " AND EXEC_POS_SEQ = @seq";

        //        IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

        //        while (rdr.Read())
        //        {
        //            Info.Seq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
        //            Info.ExecName = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
        //            Info.ExecNameShort = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
        //            Info.Remarks = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
        //        }

        //        return Info;
        //    }
        //    catch
        //    {
        //        return null;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.CloseReader();
        //        dbManager.Dispose();
        //    }
        //}
        public IList<ExecutiveInfo> ListExecutives(Nullable<int> Seq, string RecordStatus)
        {
            string strSQL;

            List<ExecutiveInfo> infoList = new List<ExecutiveInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@RecordStatus", RecordStatus);
                dbManager.AddParameters(1, "@Seq", Seq);

                strSQL = "SELECT EXEC_POS_SEQ, EXEC_POS_NAME, EXEC_POS_NAME_SHORT, EXEC_POS_NAME_ENG"
                        + ", EXEC_POS_NAME_ENG_SHORT, REMARKS, CREATED_BY, UPDATED_BY, RECORD_STATUS"
                    + " FROM HRM_MP_EXEC_POSITIONS"
                    + " WHERE RECORD_STATUS = @RecordStatus"
                    + (Seq.HasValue ? " AND EXEC_POS_SEQ = @Seq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    ExecutiveInfo Info = new ExecutiveInfo(rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6)
                            , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7));
                    Info.RecordStatus = rdr.IsDBNull(8) ? "" : rdr.GetString(8);
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(ExecutiveInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(10);

                dbManager.AddParameters(0, "@ExecPosName", info.Name);
                dbManager.AddParameters(1, "@ExecPosNameShort", info.NameShort);
                dbManager.AddParameters(2, "@ExecPosNameEng", info.NameEng);
                dbManager.AddParameters(3, "@ExecPosNameEngShort", info.NameEngShort);
                dbManager.AddParameters(4, "@ExecPosGroupSeq", info.ExecGroupSeq);
                dbManager.AddParameters(5, "@JobFamilySeq", info.JobFamilySeq);
                dbManager.AddParameters(6, "@Remarks", info.Remarks);
                dbManager.AddParameters(7, "@RecordStatus", info.RecordStatus);
                dbManager.AddParameters(8, "@CreatedBy", info.CreatedBy);
                dbManager.AddParameters(9, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_EXEC_POSITIONS(EXEC_POS_NAME, EXEC_POS_NAME_SHORT"
                        + ", EXEC_POS_NAME_ENG, EXEC_POS_NAME_ENG_SHORT"    //, EXEC_POS_GROUP_SEQ, JOB_FAMILY_SEQ
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@ExecPosName, @ExecPosNameShort, @ExecPosNameEng, @ExecPosNameEngShort"
                        //+ ", @ExecPosGroupSeq, @JobFamilySeq"
                        + ", @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(ExecutiveInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(11);

                dbManager.AddParameters(0, "@Seq", info.Sequence);
                dbManager.AddParameters(1, "@ExecPosName", info.Name);
                dbManager.AddParameters(2, "@ExecPosNameShort", info.NameShort);
                dbManager.AddParameters(3, "@ExecPosNameEng", info.NameEng);
                dbManager.AddParameters(4, "@ExecPosNameEngShort", info.NameEngShort);
                dbManager.AddParameters(5, "@ExecPosGroupSeq", info.ExecGroupSeq);
                dbManager.AddParameters(6, "@JobFamilySeq", info.JobFamilySeq);
                dbManager.AddParameters(7, "@Remarks", info.Remarks);
                dbManager.AddParameters(8, "@RecordStatus", info.RecordStatus);
                dbManager.AddParameters(9, "@UpdatedBy", info.UpdatedBy);
                dbManager.AddParameters(10, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_EXEC_POSITIONS"
                           + " SET EXEC_POS_NAME = @ExecPosName"
                                + ", EXEC_POS_NAME_SHORT = @ExecPosNameShort"
                                + ", EXEC_POS_NAME_ENG = @ExecPosNameEng"
                                + ", EXEC_POS_NAME_ENG_SHORT = @ExecPosNameEngShort"
                                //+ ", EXEC_POS_GROUP_SEQ = @ExecPosGroupSeq"
                                //+ ", JOB_FAMILY_SEQ = @RecordStatus"
                                + ", REMARKS = @Remarks"
                                + ", RECORD_STATUS = @RecordStatus"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE EXEC_POS_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_MP_EXEC_POSITIONS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE EXEC_POS_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
